package com.kc.sjwh.other.multiDataSource;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

public class ReplicationDataSourceAspect {
	
	public void before(JoinPoint point) {
		Object target = point.getTarget();
		String method = point.getSignature().getName();

		Class<?>[] arr = target.getClass().getInterfaces();
		Class<?> clazz = null;
		if(arr.length == 0) {
			clazz = target.getClass();
		} else {
			clazz = arr[0];
		}
		
		Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();

		try {
			Method m = clazz.getMethod(method, parameterTypes);
			if (m != null && m.isAnnotationPresent(DataSource.class)) {
				DataSource source = m.getAnnotation(DataSource.class);
				ReplicationDataSourceHolder.putDataSource(source.value());
			} else {
				ReplicationDataSourceHolder.putDataSource("d1");//默认去datasource1
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
	
}
